<?php $this->load->view("admin/$adminCurrentTemplate/commonHead2"); ?>

<form id="editForm" name="editForm" method="post" action="<?= $this->input->server('REQUEST_URI') ?>">
    <ul class="sub-nav">
        <li>基本设置</li>
        <li>字段管理</li>
    </ul>
    <ul class="sub-nav-item">
        <li>
            <table width="99%" border="0" cellspacing="1" cellpadding="0" class="editFormContainer">
                <input type="hidden" name="id" id="id" value="<?php if (isset($id))
    echo $id; ?>" />
                <tr>
                    <td class="guid">启用状态：</td>
                    <td class="formItem">
                        <input type="radio" name="disabled" id="disabled-0" value="0" checked="checked" /><label for="disabled-0" <?php if (isset($disabled))
    if ($disabled == 0)
        echo ' checked'; ?>>启用</label>
                        <input type="radio" name="disabled" id="disabled-1" value="1" <?php if (isset($disabled))
    if ($disabled == 1)
        echo ' checked'; ?> /><label for="disabled-1">禁用</label>
                    </td>
                </tr>
                <tr>
                    <td class="guid">频道名称：</td>
                    <td class="formItem"><input type="text" name="title" id="title"	class="sing-line input-name" value="<?php if (isset($title))
    echo $title; ?>" /></td>
                </tr>
                <tr>
                    <td class="guid">标 识 符：</td>
                    <td class="formItem"><input type="text" name="identifier" id="identifier"	class="sing-line input-name" value="<?php if (isset($identifier))
    echo $identifier; ?>" /></td>
                </tr>
                <tr>
                    <td class="guid">附 加 表：</td>
                    <td class="formItem"><input type="text" name="subjoinTable" id="subjoinTable" class="sing-line input-name" value="<?php if (isset($subjoinTable))
    echo $subjoinTable; ?>" /></td>
                </tr>
                <tr>
                    <td class="guid">投稿标题：</td>
                    <td class="formItem"><input type="text" name="useTitle" id="useTitle" class="sing-line input-name" value="<?php if (isset($useTitle))
    echo $useTitle; ?>" /></td>
                </tr>
                <tr>
                    <td class="guid">列表附加字段：</td>
                    <td class="formItem"><input type="text" name="listfields" id="listfields" class="sing-line input-name" value="<?php if (isset($listfields))
    echo $listfields; ?>" /></td>
                </tr>
                <tr>
                    <td class="guid">档案管理地址：</td>
                    <td class="formItem"><input type="text" name="adminMainCon" id="adminMainCon" class="sing-line input-name" value="<?php if (isset($adminMainCon))
    echo $adminMainCon; ?>" /></td>
                </tr>
                <tr>
                    <td class="guid">添加/修改地址：</td>
                    <td class="formItem"><input type="text" name="adminEditCon" id="adminEditCon" class="sing-line input-name" value="<?php if (isset($adminEditCon))
    echo $adminEditCon; ?>" /></td>
                </tr>
                <tr>
                    <td class="guid">用户管理地址：</td>
                    <td class="formItem"><input type="text" name="userMainCon" id="userMainCon" class="sing-line input-name" value="<?php if (isset($userMainCon))
    echo $userMainCon; ?>" /></td>
                </tr>
                <tr>
                    <td class="guid">用户发布地址：</td>
                    <td class="formItem"><input type="text" name="userEditCon" id="userEditCon" class="sing-line input-name" value="<?php if (isset($userEditCon))
    echo $userEditCon; ?>" /></td>
                </tr>
            </table>
        </li>
        <li>
            <table width="99%" border="0" cellspacing="1" cellpadding="0" class="editFormContainer field-list">
                <tr>
                    <td colspan='5'><strong>模型字段配置(可视化模式)：</strong><a href="#" class="add-field red" title="添加字段">添加字段</a></td>
                </tr>
                <tr class="guid">
                    <td>表单提示文字</td>
                    <td>数据字段名</td>
                    <td>数据类型</td>
                    <td>表单类型</td>
                    <td>维护</td>
                </tr>
                <tr>
                    <td colspan='5'>下面列出的字段是附加表的字段，除此外，它还包含主表<strong><em class='red'><?php if (isset($db_table_prefix))
    echo $db_table_prefix; ?>archives</em></strong>的所有字段</td>
                </tr><?php
if (isset($fieldsetList))
{
    foreach ($fieldsetList as $item_key => $item_value)
    {
        ?>
                        <tr class="item">
                            <td><?php echo $item_value['itemname'] ?></td>
                            <td><?php echo $item_key ?></td>
                            <td><?php echo $datatype[$item_value['type']] ?></td>
                            <td><?php
                        if (!$item_value['autofield'])
                            echo '固化字段';
                        else
                            echo '自动表单';
        ?></td>
                            <td><?php
                        if (!$item_value['autofield'])
                        {
            ?>
                                    <a class='edit-field' href="<?php
                           echo site_url('admin/channel/field');
                           if (isset($id))
                               echo '/' . $id;
                           echo '/' . $item_key;
                           ?>" title="修改字段"><img src="<?php echo base_url(); ?>template/admin/default/images/gtk-edit.png" /></a>
        <?php }else
        { ?>
                                    <a class='edit-field' href="<?php
            echo site_url('admin/channel/field');
            if (isset($id))
                echo '/' . $id;
            echo '/' . $item_key;
            ?>" title="修改字段"><img src="<?php echo base_url(); ?>template/admin/default/images/gtk-edit.png" /></a>
                                    <a class='del-field' href="<?php
            echo site_url('admin/channel/delField');
            if (isset($id))
                echo '/' . $id;
            echo '/' . $item_key;
            ?>" title="删除字段"><img src="<?php echo base_url(); ?>template/admin/default/images/gtk-del.png" /></a>
            <?php
        }
        ?></td>
                        </tr><?php
    }
}else
{
    ?>
                    <tr>
                        <td colspan='5' style="text-align:center;">暂无字段，点击<a href="#" class="add-field red" title="添加字段"><strong><em>这里</em></strong></a>以可视化的模式添加字段</td>
                    </tr>
    <?php
}
?>
                <tr>
                    <td colspan='5'><strong>模型字段配置(文本模式)：</strong>目前文本模式暂不能进行编辑！ </td>
                </tr>
                <tr>
                    <td colspan='5'><textarea name="fieldset" id="fieldset" class="multi-line" style="width:100%; margin:0; padding:0; height:200px;" readonly="readonly"><?php if (isset($fieldset))
    echo $fieldset; ?></textarea></td>
                </tr>
            </table>
        </li>
    </ul>
</form><?php $this->load->view("admin/$adminCurrentTemplate/commonFoot2"); ?>
<script type="text/javascript">
    var tvName = '<?php if (isset($tvName))
    echo $tvName; ?>';
    $(function(){
        dg.addBtn('subBtn','提 交',subForm);
        $('.sub-nav > li').bind('click',subNav).eq(0).click();
        if($('#id').val().length==0){
            $('.sub-nav > li').eq(1).hide();
        }
        $('.add-field').bind('click',editField);
        $('.edit-field').bind('click',editField);
        $('.del-field').bind('click',delField);
        $('#title').bind('blur',titleBlur);
        $('#identifier').bind('blur',identifierBlur);
        $('#subjoinTable').bind('blur',subjoinTableBlur);
    })

    var titleBlur = function(){
        var id = $('#id').val();
        var title = $.trim($(this).val());
        $.ajax({
            type:'POST',
            url:getInfo + '/check/',
            data:'tvName=' + tvName + '&fieldName=title&fieldValue=' + title + '&id=' + id,
            success:function(returnValue){
                if(parseInt(returnValue)>0){
                    subMess(208,108,'频道名称错误','<span class="red">对不起，频道名称已经存在！</span>',$('#title'));
                    return false;
                }
            }
        });
    }

    var identifierBlur = function(){
        var id = $('#id').val();
        var identifier = $.trim($('#identifier').val());
        $.ajax({
            type:'POST',
            url:getInfo + '/check/',
            data:'tvName=' + tvName + '&fieldName=identifier&fieldValue=' + identifier + '&id=' + id,
            success:function(returnValue){
                if(parseInt(returnValue)>0){
                    subMess(208,108,'标识符错误','<span class="red">对不起，标识符已经存在！</span>',$('#identifier'));
                    return false;
                }
            }
        });
    }

    var subjoinTableBlur = function(){
        var id = $('#id').val();
        var subjoinTable = $.trim($(this).val());
        $.ajax({
            type:'POST',
            url:getInfo + '/check/',
            data:'tvName=' + tvName + '&fieldName=subjoinTable&fieldValue=' + subjoinTable + '&id=' + id,
            success:function(returnValue){
                if(parseInt(returnValue)>0){
                    subMess(208,108,'附加表名称错误','<span class="red">对不起，附加表已经存在！</span>',$('#subjoinTable'));
                    return false;
                }
            }
        });
    }

    var subNav = function(){
        var id = $('#id').val();
        var index = $(this).parent().children().index(this);
        $(this).addClass('cur').siblings().removeClass('cur');
        $('.sub-nav-item > li').eq(index).show().siblings().hide();
        if(index==1){
            var newHeight = $('body').height() + 78;
            dg.reDialogSize(600,newHeight);	
            if(id.length>0)
                dg.SetTitle('修改内容模型——字段管理');
            else
                dg.SetTitle('添加内容模型——字段管理');
        }else{
            dg.reDialogSize(600,370);
            if(id.length>0)
                dg.SetTitle('修改内容模型——基本设置');
            else
                dg.SetTitle('添加内容模型——基本设置');
        }
        dg.SetPosition('center','center');
    }

    var editField = function(){
        var idValue = $('#id').val();
        var subjoinTable = $('#subjoinTable').val();
        if(idValue.length==0){
            if(subjoinTable.length==0){						
                subMess(300,108,'附加表字段错误','<span class="red">对不起，附加表不能为空，请输入附加表！</span>',$("#subjoinTable"));
                $('.sub-nav > li').eq(0).click();
                return false;
            }
        }
        var thisHref = $(this).attr('href');
        if(thisHref=='#'){
            thisHref = '<?php echo site_url("admin/channel/field"); ?>' + '/' + idValue;
        }
        if($.browser.msie){
            var lockedScroll = true;
        }else if($.browser.mozilla){
            var lockedScroll = false;
        }else if($.browser.safari){
            var lockedScroll = false;
        }else{
            var lockedScroll = false;
        }
        var addFieldDialog = new dg.curWin.$.dialog({
            id:'fieldDialog',
            parent:dg,
            width:480,
            height:320,
            title:'模型字段配置(可视化模式)——修改字段',
            cover:true,
            iconTitle:false,
            rang:true,
            maxBtn:false,
            resize:false,
            lockScroll:lockedScroll,
            autoPos:true,
            cancelBtn:false,
            page:thisHref
        });
        addFieldDialog.ShowDialog();
        return false;
    }

    var delField = function(){
        var thisHref = $(this).attr('href');
        if($.browser.msie){
            var lockedScroll = true;
        }else if($.browser.mozilla){
            var lockedScroll = false;
        }else if($.browser.safari){
            var lockedScroll = false;
        }else{
            var lockedScroll = false;
        }
        var delFieldDialog = new dg.curWin.$.dialog({
            id:'fieldDialog',
            parent:dg,
            width:480,
            height:320,
            title:'模型字段配置(可视化模式)——删除字段',
            cover:true,
            iconTitle:false,
            rang:true,
            maxBtn:false,
            resize:false,
            lockScroll:lockedScroll,
            autoPos:true,
            cancelBtn:false,
            page:thisHref
        });
        var delFieldDialog2 = new dg.curWin.$.dialog({
            id:'fieldDialog2',
            parent:dg,
            width:280,
            height:108,
            title:'确认删除字段？？',
            cover:true,
            iconTitle:false,
            rang:true,
            maxBtn:false,
            autoPos:{left:'center',top:'center'},
            cancelBtnTxt:'确 定',
            onCancel:function(){
                delFieldDialog.ShowDialog();
                delFieldDialog2.cancel();
            },
            html:'<p class="mess-detail">确认删除字段吗？本次操作将不可恢复！</p>'
        });
        delFieldDialog2.ShowDialog();
        return false;
    }

    var subForm = function(){
        var id = $.trim($('#id').val());
        var disabled = $.trim($('#disabled').val());
        var title = $.trim($('#title').val());
        if(title.length==0){
            subMess(320,108,'名称错误','<span class="red">对不起，频道名称不能为空，请输入频道名称！</span>',$("#title"));
            return false;
        }
        var identifier = $.trim($('#identifier').val());
        if(identifier.length==0){
            subMess(300,108,'标识符错误','<span class="red">对不起，标识符不能为空，请输入标识符！</span>',$("#identifier"));
            return false;
        }
        var subjoinTable = $.trim($('#subjoinTable').val());
        if(subjoinTable.length==0){
            subMess(300,108,'附加表错误','<span class="red">对不起，附加表不能为空，请输入附加表！</span>',$("#subjoinTable"));
            return false;
        }
        var adminEditCon = $.trim($('#adminEditCon').val());
        if(adminEditCon.length==0){
            subMess(440,108,'后台添加/修改地址错误','<span class="red">对不起，后台添加/修改地址不能为空，请输入后台添加/修改地址！</span>',$("#adminEditCon"));
            return false;
        }
        //var adminMainCon = $.trim($('#adminMainCon').val());
        //if(adminMainCon.length==0){
        //	subMess(440,108,'后台档案管理地址错误','<span class="red">对不起，后台档案管理地址不能为空，请输入后台档案管理地址！</span>',$("#adminMainCon"));
        //	return false;
        //}
        $('#editForm').submit();
    }
</script>